package com.example.demo.service.impl;

import com.example.demo.dao.FoodManager;
import com.example.demo.entity.Food;
import com.example.demo.entity.MyException;
import com.example.demo.entity.ResultEnum;
import com.example.demo.service.PoultryKitchenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author cc
 * @date 2018/9/19 15:06
 */
@Service
@Transactional
public class PoultryKitchenServiceImpl implements PoultryKitchenService {
    private final FoodManager foodManager;

    @Autowired
    public PoultryKitchenServiceImpl(FoodManager foodManager) {
        this.foodManager = foodManager;
    }

    @Override
    public Food cookPoultry(Food food) {
        String foodName = food.getFoodName();
        switch (foodName) {
            case "鸡":
                return cookChicken(food);
            case "鸭":
                return cookDuck();
            default:
                return null;
        }
    }

    private Food cookChicken(Food food) {
        Food chickenInWareHouse = foodManager.queryFoodByName("生鸡");
        Long foodCount = food.getCount();
        if (chickenInWareHouse.getCount() <= foodCount) {
            throw new MyException(ResultEnum.FOOD_ERROR);
        }

        //从仓库拿走1只鸡
        foodManager.takeFood(chickenInWareHouse.getFoodNo(), -foodCount);
        Food chicken = new Food(chickenInWareHouse.getFoodNo(), chickenInWareHouse.getFoodName(), foodCount);

        try {
            Thread.sleep(500);
            chicken.setFoodName("煮好的鸡");
        } catch (InterruptedException e) {
            throw new MyException(e);
        }
        return chicken;
    }

    // TODO: 2018/9/22
    private Food cookDuck() {
        return null;
    }
}
